package com.cloudpivot.receivales;

import cn.hutool.core.img.ImgUtil;
import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.basic.controller.DingBasicController;
import com.cloudpivot.receivales.ding.basic.service.impl.HttpAPIServiceForDing;
import com.cloudpivot.receivales.ding.scenegroup.controller.ScenegroupController;
import com.cloudpivot.receivales.ding.scenegroup.domain.*;
import com.cloudpivot.receivales.ding.scenegroup.service.DriveSpacesService;
import com.cloudpivot.receivales.ding.scenegroup.service.ScenegroupService;
import com.cloudpivot.receivales.ding.userinfo.service.DingUserService;
import com.cloudpivot.receivales.workorder.domain.AnnotaionsDTO;
import com.cloudpivot.receivales.workorder.domain.WorkorderReportDTO;
import com.cloudpivot.receivales.workorder.service.WorkorderPoiService;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.RenderPolicy;
import com.deepoove.poi.render.processor.Visitor;
import com.deepoove.poi.template.ElementTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.hibernate.jdbc.Work;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.List;

import static com.cloudpivot.receivales.ding.util.DingUtil.listToString;

@SpringBootTest
class CloudpivotApplicationTestForWorkorder {

    @Value("${workorder.modepath}")
    private String MODEPATH;

    @Autowired
    private WorkorderPoiService workorderPoiService;

    @Autowired
    private HttpAPIServiceForDing httpAPIServiceForDing;

    @Test
    public void testFirstdoc() {
        WorkorderReportDTO entity = new WorkorderReportDTO();
        entity.setNew_actual_problem_description("api 推送的content");
        entity.setNew_num("测试Poitl的功能");
        workorderPoiService.createWorkorderReportFile(entity);
    }

    /**
     * 测试 文档载入签名
     */
    @Test
    public void testDocSignOutFile() {
        try {
            File file = new File(MODEPATH+"yaotengyisign.png");
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            inputStream.close();
            Base64.Encoder encoder = Base64.getEncoder();
            String base64 = encoder.encodeToString(buffer);
            //图片旋转
            BufferedImage inImage = ImgUtil.toImage(base64);
            Image outImage=ImgUtil.rotate(inImage,-90);
            String outBase64 = ImgUtil.toBase64(outImage, "png");
            System.out.println("base64 转换:" + Strings.isNotBlank(outBase64));

            //创建 模版替换对象
            WorkorderReportDTO entity = new WorkorderReportDTO();
            entity.setNew_num("Test-1001-0101");
            entity.setNew_accountname("测试的客户");
            entity.setNew_actual_problem_description("测试的描述");
            //子表
            WorkorderReportDTO.WxItem item = new WorkorderReportDTO.WxItem();
            item.setNew_name("Test");
            item.setNew_add_code("10001");
            item.setNew_productname("testProduct");
            item.setNew_number("1");
            List<WorkorderReportDTO.WxItem> wxlist = new ArrayList<>();
            wxlist.add(item);

            entity.setWxItems(wxlist);

            workorderPoiService.createWorkorderFileBySignbase64(entity, outBase64);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 测试 文档载入签名
     */
    @Test
    public void testDocSifn() {
        try {
            File file = new File(MODEPATH+"testSign.png");
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            inputStream.close();
            Base64.Encoder encoder = Base64.getEncoder();
            String base64 = encoder.encodeToString(buffer);
            System.out.println("base64 转换:" + Strings.isNotBlank(base64));

            //创建 模版替换对象
            WorkorderReportDTO entity = new WorkorderReportDTO();
            entity.setNew_num("Test-1001-0101");
            entity.setNew_accountname("测试的客户");
            entity.setNew_actual_problem_description("测试的描述");
            //子表
            WorkorderReportDTO.WxItem item = new WorkorderReportDTO.WxItem();
            item.setNew_name("Test");
            item.setNew_add_code("10001");
            item.setNew_productname("testProduct");
            item.setNew_number("1");
            List<WorkorderReportDTO.WxItem> wxlist = new ArrayList<>();
            wxlist.add(item);

            entity.setWxItems(wxlist);
            workorderPoiService.createWorkorderStreamBySignbase64(entity, base64);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void getSignImg(){
        try {
            String url="http://172.17.100.12/api/api/aliyun/download?refId=5534e3283e9f49be93fe38e10d1f7589";

            Base64.Encoder encoder = Base64.getEncoder();
            byte[] ret=httpAPIServiceForDing.doGetToBytes(url);
            String base4=encoder.encodeToString(ret);
            System.out.println(base4.length());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void getFileImg(){
        try {
            File file = new File(MODEPATH+"signsture.png");
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            inputStream.close();
            Base64.Encoder encoder = Base64.getEncoder();
            String base64 = encoder.encodeToString(buffer);
            System.out.println(base64.length());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void getCrmId() {
        String crmid = "f4e3e447-c0ea-ec11-8ac9-fd3d50f160fd";
        WorkorderReportDTO entity=workorderPoiService.getWorkOrderDetail(crmid);
        System.out.println(JSONObject.toJSONString(entity));
    }

    @Test
    public void annotationsForTest() {
        try {
            File file = new File(MODEPATH+"signsture.png");
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            inputStream.close();
            Base64.Encoder encoder = Base64.getEncoder();
            String base64 = encoder.encodeToString(buffer);
            AnnotaionsDTO entity = new AnnotaionsDTO();
            entity.setDocumentbody(base64);
            entity.setFilename("signsture.png");
            entity.setIsdocument(true);
            entity.setObjecttypecode("new_work_order");
            entity.setObjectid("/new_work_orders(c2519c7e-29f1-ec11-8ac9-fd3d50f160fd)");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片 旋转测试
     */
    @Test
    public void ropicForTest() {
        try {
            File inFile = new File(MODEPATH+"signsture.png");
            File outFile = new File(MODEPATH+"out/"+System.currentTimeMillis()+".png");
            FileInputStream inputStream = new FileInputStream(inFile);
            byte[] buffer = new byte[(int) inFile.length()];
            inputStream.read(buffer);
            inputStream.close();
            Base64.Encoder encoder = Base64.getEncoder();
            String base64 = encoder.encodeToString(buffer);
            BufferedImage inImage = ImgUtil.toImage(base64);
            ImgUtil.rotate(inImage, -90,outFile);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
